#include "stdafx.h"
#include "ModuleMgr.h"

ModuleMgr::ModuleMgr()
{
	m_currmodule = nullptr;
}

ModuleMgr::~ModuleMgr()
{
	m_close();
}

void ModuleMgr::AddModule(const std::string &name, IModule *scene)
{
	m_moduleList.insert(std::pair<std::string, IModule*>(name, scene));
}

void ModuleMgr::SetActiveModule(const std::string &scene)
{
	if ( m_currmodule )
		m_currmodule->close();

	auto it = m_moduleList.find(scene);
	if ( it == m_moduleList.end() )
		return;

	m_currmodulename = it->first;
	m_currmodule = it->second;

	m_currmodule->setmgr(this);
	m_currmodule->init(m_currmodulename);
}

bool ModuleMgr::Frame()
{
	if ( !m_currmodule )
		return false;

	return m_currmodule->frame();
}

void ModuleMgr::m_close()
{
	for ( auto it = m_moduleList.begin(); it != m_moduleList.end(); ++it )
	{
		if ( it->second )
		{
			it->second->close();
			delete it->second;
			it->second = nullptr;
		}
	}

	m_moduleList.clear();
}
